﻿using com.fmtx.IDal;
using com.fmtx.Model;
using fmtx.Framework.DALFactory;
using fmtx.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace website.Areas.Write.Controllers
{
    public class ImageController : BaseController
    {
        //
        // GET: /Write/Image/
        private IPictureType pictureType = DataAccess<IPictureType>.GetObject();

        private IPicture picture = DataAccess<IPicture>.GetObject();

        #region 相册列表
        /// <summary>
        /// 相册列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var models = pictureType.Query(userInfo.Uid);
            return View(models);
        } 
        #endregion

        #region 相册
        /// <summary>
        /// 相册
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Detail(int id)
        {
            ViewBag.bean = pictureType.Get(id);
            var models = picture.QueryByUser(userInfo.Uid, id);
            return View(models);
        } 
        #endregion

        #region 图片编辑
        /// <summary>
        /// 图片编辑
        /// </summary>
        /// <param name="typeid"></param>
        /// <param name="images"></param>
        /// <returns></returns>
        public ActionResult SaveImage(int typeid, string images)
        {
            ViewBag.typeid = typeid;
            var strs = images.Split(',');
            return View(strs);
        }
        #endregion

        #region 保存图片列表
        /// <summary>
        /// 保存图片列表
        /// </summary>
        /// <param name="models"></param>
        /// <param name="typeid"></param>
        /// <returns></returns>
        public ActionResult DoSaveImg(List<Picture> Photo, int typeid)
        {
            var type = pictureType.Get(typeid);
            if (Photo != null)
            {
                foreach (var item in Photo)
                {
                    string file = StaticCs.SaveTempPic(item.PicAddress);
                    if (string.IsNullOrEmpty(type.FrontCover))
                    {
                        type.FrontCover = file;
                        pictureType.Update(type);
                    }
                    item.TypeId = typeid;
                    item.PicAddress = file;
                    item.Scale = ImageFactory.GetScale(Server.MapPath(StaticCs.VirtualPath + file));
                    item.Uid = userInfo.Uid;
                    item.InTime = DateTime.Now;
                    item.Content = item.Content.FileDirt();
                    item.Title = item.Title.FileDirt();
                    item.Statu = Approved.IsPass;
                    picture.Add(item);
                    PictureCache.Insert(item);
                }
            }
            StaticCs.RemoveUserFolder(userInfo.Uid);
            return RedirectToAction("Detail", new { id=typeid});
        }
        #endregion

    }
}
